layui.use(['element', 'layer', 'form', 'laypage'], function () {
    var element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块

    // 设置弹窗登录框
    var $ = layui.jquery,
        layer = layui.layer; //独立版的layer无需执行这一句

    //表单
    var form = layui.form;

    //监听提交
    form.on('submit(formDemo)', function (data) {
        layer.msg(JSON.stringify(data.field));
        return false;
    });
    $(".user-left-menu").find("a").click(function () {
        $(".user-left-menu").find("a").removeClass("active");
        $(this).addClass("active");
        var id = $(this).attr("name");
        changetag(id);
    });
    //无内容提示
    no();

    function no() {
        if ($(".user-main-item .user-content ul li").length == "0") {
            $(".user-main-item .user-content").hide();
            $(".nothing").fadeIn("slow");
        } else {
            $(".user-main-item .user-content").show();
            $(".nothing").fadeOut("slow");
        }
    }

    function changetag(id) {
        switch (id) {
            case "1":
                if ($(".user-main-menu").find("#user-dz").css("display") == "block") {
                    break;
                } else {
                    getmsg1();
                    $(".user-main-menu").find(".user-main-item").hide();
                    $(".user-main-menu").find("#user-dz").fadeIn("slow");
                    break;
                }
            case "2":
                if ($(".user-main-menu").find("#user-sc").css("display") == "block") {
                    break;
                } else {
                    getmsg2();
                    $(".user-main-menu").find(".user-main-item").hide();
                    $(".user-main-menu").find("#user-sc").fadeIn("slow");
                    break;
                }
            case "3":
                if ($(".user-main-menu").find("#user-pl").css("display") == "block") {
                    break;
                } else {
                    getmsg3();
                    $(".user-main-menu").find(".user-main-item").hide();
                    $(".user-main-menu").find("#user-pl").fadeIn("slow");
                    break;
                }
            case "4":
                if ($(".user-main-menu").find("#sys-info").css("display") == "block") {
                    break;
                } else {
                    getmsg4();
                    $(".user-main-menu").find(".user-main-item").hide();
                    $(".user-main-menu").find("#sys-info").fadeIn("slow");
                    break;
                }
            default:
                break;
        }
    }

    function getmsg1() {
        $.ajax({
            url: '/User/zanmsg',
            success(data) {
                $(".user-content ul").empty();
                if (data.messages.length != 0) {
                    $(".user-main-item .user-content").show();
                    $(".nothing").fadeOut("slow");
                    for (var o in data.messages) {
                        $(".user-content ul").append(
                            "<li>" +
                            "<div class='message-item'>" +
                            "<span id='name'>" + data.messages[o]["iid"] + "</span>" +
                            "<span> 赞了你的博客</span>" +
                            "<span id='delete' onclick='delmsg(this);' value=" + data.messages[o]["mid"] + "><i class='layui-icon'>&#xe640;</i> </span>" +
                            "<div class='message-content'>" +
                            "<a href='/Blog/blogdetail/" + data.messages[o]["bid"] + "'>" +
                            "<span id='content' >" + data.blogs[o]["title"] + "</span></a>" +
                            "<span id='time'>" + data.blogs[o]["date"] + "</span>" +
                            "</div></div></li>"
                        );
                    }
                } else {
                    $(".user-main-item .user-content").hide();
                    $(".nothing").fadeIn("slow");
                }
            }
        });
    }

    function getmsg2() {
        $.ajax({
            url: '/User/colmsg',
            success(data) {
                $(".user-content ul").empty();
                if (data.messages.length != 0) {
                    $(".user-main-item .user-content").show();
                    $(".nothing").fadeOut("slow");
                    for (var o in data.messages) {
                        $(".user-content ul").append(
                            "<li>" +
                            "<div class='message-item'>" +
                            "<span id='name'>" + data.messages[o]["iid"] + "</span>" +
                            "<span> 收藏了你的博客</span>" +
                            "<span id='delete' onclick='delmsg(this);' value=" + data.messages[o]["mid"] + "><i class='layui-icon'>&#xe640;</i> </span>" +
                            "<div class='message-content'>" +
                            "<a href='/Blog/blogdetail/" + data.messages[o]["bid"] + "'>" +
                            "<span id='content' >" + data.blogs[o]["title"] + "</span></a>" +
                            "<span id='time'>" + data.blogs[o]["date"] + "</span>" +
                            "</div></div></li>"
                        );
                    }
                } else {
                    $(".user-main-item .user-content").hide();
                    $(".nothing").fadeIn("slow");
                }
            }
        });
    }

    function getmsg3() {
        $.ajax({
            url: '/User/commsg',
            success(data) {
                $(".user-content ul").empty();
                if (data.messages.length != 0) {
                    $(".user-main-item .user-content").show();
                    $(".nothing").fadeOut("slow");
                    for (var o in data.messages) {
                        $(".user-content ul").append(
                            "<li>" +
                            "<div class='message-item'>" +
                            "<span id='name'>" + data.messages[o]["iid"] + "</span>" +
                            "<span> 评论了你的博客</span>" +
                            "<span id='delete' onclick='delmsg(this);' value=" + data.messages[o]["mid"] + "><i class='layui-icon'>&#xe640;</i> </span>" +
                            "<div class='message-content'>" +
                            "<a href='/Blog/blogdetail/" + data.messages[o]["bid"] + "'>" +
                            "<span id='content' >" + data.blogs[o]["title"] + "</span></a>" +
                            "<span id='time'>" + data.blogs[o]["date"] + "</span>" +
                            "</div></div></li>"
                        );
                    }
                } else {
                    $(".user-main-item .user-content").hide();
                    $(".nothing").fadeIn("slow");
                }
            }
        });
    }

    function getmsg4() {
        $.ajax({
            url: '/User/repmsg',
            success(data) {
                $(".user-content ul").empty();
                if (data.messages.length != 0) {
                    $(".user-main-item .user-content").show();
                    $(".nothing").fadeOut("slow");
                    for (var o in data.messages) {
                        $(".user-content ul").append(
                            "<li>" +
                            "<div class='message-item'>" +
                            "<span id='name'>" + data.messages[o]["iid"] + "</span>" +
                            "<span> 回复了你 </span>" +
                            "<span style='color: #999'> " + data.comments[o]["content"] + " </span>" +
                            "<span id='delete' onclick='delmsg(this);' value=" + data.messages[o]["mid"] + "><i class='layui-icon'>&#xe640;</i> </span>" +
                            "<div class='message-content'>" +
                            "<span id='content'> " + data.replies[o]["content"] + "</span>" +
                            "<span id='time'>" + data.replies[o]["date"] + "</span>" +
                            "</div></div></li>"
                        )
                        ;
                    }
                } else {
                    $(".user-main-item .user-content").hide();
                    $(".nothing").fadeIn("slow");
                }
            }
        });
    }

    //删除消息
    delmsg = function (obj) {
        console.log("删除" + $(obj).attr("value"));
        $.ajax({
            url: '/User/delmsg/' + $(obj).attr("value"),
            success(data) {
                $(obj).parent().parent().hide("slow");
                layer.msg(data);
            }
        });
        if ($(obj).parent().parent().parent().find("li").length == 0) {
            $(".user-main-item .user-content").hide();
            $(".nothing").fadeIn("slow");
        }
    };
    //清除消息
    cleanmsg = function (obj) {
        var li = "";
        $(obj).parent().next().find(" ul li").each(function () {
            li += $(this).find("#delete").attr("value") + ",";
        });
        $(obj).parent().next().find(" ul").empty();
        $.ajax({
            url: '/User/cleanmsg',
            data: {li: li},
            success(data) {
                layer.msg(data);
            }
        });
        $(".user-main-item .user-content").hide();
        $(".nothing").fadeIn("slow");
    };

    //是否显示分页
    var len = $(".user-main-item .user-content ul li");
    if (len.length > 9) {
        $(".user-main-item #page").show();
    }
    // 分页
    var laypage = layui.laypage;

    //执行一个laypage实例
    laypage.render({
        elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
        , count: 6,//数据总数，从服务端得到
        limit: 6,
        limits: 3
    });
});
